package com.jason.kotlin.float

import android.os.Bundle
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.text.method.NumberKeyListener
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.jason.kotlin.R
import com.jason.kotlin.utils.IDCardUtil
import com.jason.library.common.utils.ToastUtils
import com.jason.library.widget.SimpleTextWatcher
import com.jason.library.widget.floatedit.CbyFloatInput
import kotlinx.android.synthetic.main.activity_float_input.*
import java.util.regex.Pattern

class FloatInputActivity : AppCompatActivity() {

    private lateinit var floatInput : CbyFloatInput

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_float_input)
        floatInput = CbyFloatInput(this)
        setContent()
        setListener()
    }

    private fun setContent() {
        tv_title.text = "实名认证"
        tv_policy.text = "根据《国家新闻出版署关于防止未成年人沉迷网络游戏的通知》要求，需要核实您的真实身份。该信息仅为认证您的身份，请您放心进行填写。"

    }

    private fun setListener() {
        iv_close.setOnClickListener {
            finish()
        }


        setTextWatcher(edit_name)
        setTextWatcher(edit_id_card)
        edit_name.isFocusable = false
        edit_id_card.isFocusable = false
        edit_id_card.keyListener = object : NumberKeyListener() {
            override fun getInputType(): Int {
                return InputType.TYPE_CLASS_PHONE
            }

            override fun getAcceptedChars(): CharArray {
                return charArrayOf('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'X')
            }
        }

        edit_name.filters = arrayOf<InputFilter>(LengthFilter(20))
        edit_id_card.filters = arrayOf<InputFilter>(LengthFilter(18))
        edit_name.setOnClickListener { floatInput.show(view_point, edit_name) }
        edit_id_card.setOnClickListener { floatInput.show(view_point, edit_id_card) }

        tv_submit_btn.isEnabled = false
        tv_submit_btn.setOnClickListener(View.OnClickListener {
            val name: String = edit_name.text.toString()
            val idCard: String = edit_id_card.text.toString()
            if (!checkName(name)) {
                ToastUtils.showToast(this@FloatInputActivity,"请输入正确的姓名")
                return@OnClickListener
            }
            if (!IDCardUtil.Verify(idCard)) {
                ToastUtils.showToast(this@FloatInputActivity,"请输入正确的身份证号")
                return@OnClickListener
            }
            ToastUtils.showToast(this@FloatInputActivity,"本地合法校验ok");

        })
    }

    private fun setTextWatcher(editText: EditText?) {
        editText?.addTextChangedListener(object : SimpleTextWatcher() {
            override fun onTextChanged() {
                val enable = edit_id_card != null && edit_id_card.length() > 0 && edit_name != null && edit_name.length() > 0
                updateButtonState(enable)
            }
        })
    }

    private fun updateButtonState(enable: Boolean) {
        tv_submit_btn.apply {
            isEnabled = enable
            setBackgroundResource(if (enable) R.drawable.float_input_bg_enable_button
            else R.drawable.float_input_bg_disable_button)
        }
    }


    companion object {
        private const val REG_HAN_ZI =
            "((?![\\u3000-\\u303F])[\\u2E80-\\uFE4F]|\\·)*(?![\\u3000-\\u303F])[\\u2E80-\\uFE4F](\\·)*$"

    }

    private fun checkName(name: String): Boolean {
        val pattern = Pattern.compile(REG_HAN_ZI)
        val matcher = pattern.matcher(name)
        return matcher.matches()
    }
}